package com.java.code.web.config;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

/**
 * @author weichuanyuan
 * @version 1.0
 * @date 2021/10/24 20:43
 */
public class RenameAnnotationBeanNameGenerator extends AnnotationBeanNameGenerator {
    /**
     * 为了配合proguard使用，需要自定义bean的命名策略为结合包名的，
     *
     * @param definition
     * @return
     */
    protected String buildDefaultBeanName(BeanDefinition definition) {
        String className = definition.getBeanClassName();
        if (className.contains("九")) {
            //proguard混淆
            className = className.replaceAll("\\.", "_");
            System.out.println("混淆之后的命名:" + className);
        } else {
            //普通代码
            className = className.substring(className.lastIndexOf(".") + 1);
            if (className.toLowerCase().endsWith("impl")) {
                className = className.substring(0, className.length() - 4);
            }
            if ((className.toLowerCase().endsWith("service") || className.toLowerCase().endsWith("dao")) == false) {
                return super.buildDefaultBeanName(definition);
            }
        }
        return className;
    }
}
